<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 上午10:34
 */

class ModMenu extends Model {

    private $table = 'tblMenu';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbAuth;
    }

    public function select($params=array()){
        $where  = ' WHERE 1 ';
        $args   = array();

        if(!empty($params['pid'])){
            $where .= ' AND ParentID=:ParentID ';
            $args[':ParentID'] = $params['pid'];
        }

        if(!empty($params['search'])){
            $where .= ' AND MenuName LIKE :MenuName ';
            $args[':MenuName'] = '%'.$params['search'].'%';
        }


        $sql = "SELECT MenuID,MenuName,ParentID,MenuUrl,MenuIcon,Function,Remark,Status,Visible,SortNum
                FROM {$this->table} {$where} ORDER BY ParentID, SortNum";

        $result = $this->getDbConnection()->getAll($sql, $args);
        return $result;
    }
    
    public function page($params){
    	$table = $this->table;
    	$where = ' WHERE 1';
    	$args = array();

        if(!empty($params['pid'])){
            $where .= ' AND ParentID=:ParentID ';
            $args[':ParentID'] = $params['pid'];
        }

        if(!empty($params['search'])){
            $where .= ' AND MenuName LIKE :MenuName ';
            $args[':MenuName'] = '%'.$params['search'].'%';
        }
    	 
    	$c_sql = "select count(1) as cnt from {$table} {$where}";
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;
    
    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	$fields = "MenuID,MenuName,ParentID,MenuUrl,MenuIcon,Function,Remark,Status,Visible,SortNum";
    	$sql = "SELECT {$fields} FROM {$table} {$where} {$order} {$limit}";
    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	return " ORDER BY {$order[0]} {$sort} ";
    }
    /**
     * 获得一个用户的菜单信息
     * @param $userId
     * @param bool $appId
     * @return array
     */
    public static function getUserMenu($userId){

        $sql = "SELECT MenuID FROM tblUserMenuMap
                    WHERE UserID=:UserID";

        $args = array(':UserID' => $userId);
        $tmp = Yii::app()->dbAuth->getAll($sql, $args);
        $result = array();
        if(!empty($tmp) && is_array($tmp)){
            foreach($tmp as $v){
                $result[] = $v['MenuID'];
            }
        }

        return $result;
    }
    
    /**
     * 获得一个角色的菜单信息
     * @param $userId
     * @param bool $appId
     * @return array
     */
    public static function getRoleMenu($roleId){
    
    	$sql = "SELECT MenuID FROM tblrolemenumap
                    WHERE RoleID=:RoleID";
    
    	$args = array(':RoleID' => $roleId);
    	$tmp = Yii::app()->dbAuth->getAll($sql, $args);
    	$result = array();
    	if(!empty($tmp) && is_array($tmp)){
    		foreach($tmp as $v){
    			$result[] = $v['MenuID'];
    		}
    	}
    
    	return $result;
    }

    public function getMaxSort($pid){
        $sql = "SELECT max(SortNum) AS MaxSort FROM {$this->table} WHERE ParentID=:pid";
        $args = array(':pid' => $pid);

        $result = $this->getDbConnection()->getOne($sql, $args);

        return isset($result['MaxSort']) ? $result['MaxSort'] : 0;
    }

} 